package com.example.management.system.controller

import com.example.management.common.utils.R
import com.example.management.system.domain.UserOnline
import com.example.management.system.service.SessionService
import java.security.Principal

import org.apache.shiro.session.Session
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.servlet.mvc.support.RedirectAttributes


@RequestMapping("/sys/online")
@Controller
class SessionController {
    @Autowired
    internal var sessionService: SessionService? = null

    @GetMapping
    fun online(): String {
        return "system/online/online"
    }

    @ResponseBody
    @RequestMapping("/list")
    fun list(): List<UserOnline> {
        return sessionService!!.list()
    }

    @ResponseBody
    @RequestMapping("/forceLogout/{sessionId}")
    fun forceLogout(@PathVariable("sessionId") sessionId: String, redirectAttributes: RedirectAttributes): R {
        try {
            sessionService!!.forceLogout(sessionId)
            return R.ok()
        } catch (e: Exception) {
            e.printStackTrace()
            return R.error()
        }

    }

    @ResponseBody
    @RequestMapping("/sessionList")
    fun sessionList(): Collection<Session> {
        return sessionService!!.sessionList()
    }


}
